package com.atguigu.ucenterservice.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.vo.RegisterVo;
import com.atguigu.ucenterservice.mapper.UcenterMemberMapper;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-18
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Override
    public String login(UcenterMember member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //校验参数
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw  new GuliException(20001, "账号或密码为空");
        }

        //获取手机号
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);

        UcenterMember mobileMember = baseMapper.selectOne(wrapper);

        //判断手机号是否为空
        if (mobileMember == null){

            throw new GuliException(20001, "登录失败手机号不存在");
        }
        //校验密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5

        if(! MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new GuliException(20001, "登录失败,密码不正确 !");
        }

        //校验是否被禁用
        Boolean isDisabled = mobileMember.getIsDisabled();
        if (isDisabled){
            throw new GuliException(20001, "登录失败, 账号被禁用");
        }

        //使用JWT生成token字符串
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getPassword());

        return jwtToken;
    }

    @Override
    public Boolean register(RegisterVo registerVo) {

        //获取注册的数据
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code)){
            throw new GuliException(20001, "注册失败");
        }
        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001, "注册失败验证码不正确");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);

        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0 ){
            throw new GuliException(20001, "注册失败,手机号已存在");
        }
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setNickname(nickname);

        //是否禁用, 否
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("https://common-service-bucket.oss-cn-hangzhou.aliyuncs.com/logo.png");

        int insert = baseMapper.insert(ucenterMember);


        return insert > 0;
    }

    @Override
    public Integer countRegisterDay(String day) {


        return baseMapper.selectRegisterCount(day);
    }
}
